package multiples_colas;
// OOSimL v 1.1 File: Multiples_colas.java, Mon Mar 19 17:11:51 2012
 
import java.util.*; 
import java.io.*; 
import java.text.DecimalFormat; 
import psimjava.*; 
/** 
  description
  A multi-server multi-queue model of a Car-wash system
  Arriving cars (customers) join the shortest queue to wait 
  for service. Each server is a car-wash machine that 
  services one car at a time. Service consists of a 
  complete external wash of a car.
  
  OOSimL model J. Garrido. Updated march 18, 2012
  Main class: Multiples_colas
  
  This class sets up simulation parameters and creates the
  relevant objects.
  There are four types of active objects of classes: 
    Multiples_colas, Server, Arrivals, and Car.
  There are several passive objects, the car_queues of class 
  Squeue, one queue for every server.
  The arrivals object creates the various car objects that 
  arrive at random intervals. 
  Random variables: inter-arrival period of a negative 
   exponential distribution and service period of a 
   Normal distribution.
   
  File: Multiples_colas.osl
  */
 public  class Multiples_colas  extends psimjava.Process     {
static Scanner scan = new Scanner (System.in);
 private  static boolean  guiflag =  true ; 
  // files for reporting 
 private  static PrintWriter statf; 
 // statistics 
 private  static PrintWriter tracef; 
 // trace 
 // number of servers 
 public  static int  num_serv = 3; 
  // input car queue size 
 //  
 public  static int  totalQSize = Integer.MAX_VALUE; 
  // simulation period    
 // 
 public  static double  simPeriod = 1000.0; 
  // time to close arrival of cars 
 public  static double  close_arrival = 1000.0; 
  // 
 public  static String  simname = "Multiserver Multi-queue Carwash"; 
  // mean car inter-arrival interval 
 // 
 // car service interval  
 public  static double  MeanInterarrival = 4.5; 
  // Uniform distribution  
 public  static double  minServiceDur = 2.25; 
  // 
 // Accumulative variables 
 // 
 public double  maxServiceDur = 16.50; 
  // accumulated customer service time 
 public  static double  custServiceTime = 0.0; 
  // accumulated customer sojourn time 
 public  static double  custSojournTime = 0.0; 
  // accumulated car waiting time   
 //  
 public  static double  custWaitTime = 0.0; 
  // accum server idle time 
 // 
 public  static double  accum_serv[]; 
  // current number of customers serviced 
 public  static int  num_serviced = 0; 
  // current number of cars that arrived  
 public  static int  num_arrived = 0; 
  // current number of cars rejected 
 public  static int  num_rejected = 0; 
  //  
 // 
 // Simulation model essential objects 
 // 
 // Queues for the model 
 // 
 public  static Simulation sim; 
 // array of simple queues for arriving cars 
 //  
 // References for the active objects of the model 
 public  static Squeue car_queue[]; 
 // 
 public  static Server wash_machine[]; 
 public  static Arrivals genCust; 
 public  static Multiples_colas shop; 
 public SimModel model; 
/** 
      description
         This is the constructor for the main class
         */
 //  
 public Multiples_colas( String ss) { 
super(ss); 
 } // end initializer 
 // 
 public static void main(String[] args) {
 int  i; 
 guiflag =   false ;
 // set-up simulation with an object of class simulation 
 // 
 sim = new Simulation( "Multi-server multi-queue Car-Wash Model" );
 // setup files for reporting 
 Simulation.set_tracef("mqcarwtrace.txt");
 // 
 Simulation.set_statf("mqcarwstatf.txt");
 // create array of queues (passive objects) 
car_queue = new Squeue [num_serv];
 // create each queue 
i =  0;
 while ( i < num_serv ) { 
car_queue [i] = new Squeue("Cust Queue" + i, totalQSize);
 i++;
 // 
 } // endwhile 
 // Create and start the main active objects of the model 
shop = new Multiples_colas(simname);
 // "Multi-server multi-queue Car Wash System" 
 shop.start();
 // 
 }  // end main 
 // This function is called by the class object with interactive I/O 
 public void  pbegin(  SimModel sm ) { 
 int  jin; 
  double  din; 
  int  i; 
 model =  sm;
 // call model.println using "Input workload parameters for Car Wash Model" 
model.println("Multi-server Multi-queue Model of a Car-Wash System"); 
 // Input parameters 
jin = model.readInt("Enter number of servers (5): "); 
 if ( jin > 0) { 
num_serv =  jin;
 } // endif 
 // 
din =  model.readDouble("Enter simulation period (1200.0): ");
 if ( din > 0.0) { 
simPeriod =  din;
 } // endif 
 // 
din = model.readDouble("Enter close time (650.5): "); 
 if ( din > 0.0) { 
close_arrival =  din;
 } // endif 
 //    
din =  model.readDouble("Enter mean inter-arrival period (2.5): ");
 if ( din > 0.0) { 
MeanInterarrival =  din;
 } // endif 
 // 
din = model.readDouble("Enter low service period (12.25): "); 
 if ( din > 0.0) { 
minServiceDur =  din;
 } // endif 
 // 
din = model.readDouble("Enter high service time (16.50): "); 
 if ( din > 0.0) { 
maxServiceDur =  din;
 // 
 } // endif 
 // set-up simulation 
 // "Multi-server Multi-queue Car-Wash Model" 
 // 
 sim = new Simulation( simname );
 // Set up report files 
 Simulation.set_tracef("mqcarwtrace.txt");
 // 
 Simulation.set_statf("mqcarwstatf.txt");
 // Create  passive objects: queues 
car_queue = new Squeue [num_serv];
 // create each queue 
i =  0;
 while ( i < num_serv ) { 
car_queue [i] = new Squeue("Cust Queue" + i, totalQSize);
 i++;
 // 
 } // endwhile 
 // start main active object in model 
 start();
 // 
 }  // end pbegin 
 // 
 public void  Main_body(   ) { 
 int  i; 
  String  pname; 
  // 
 double  machutil; 
  // formatting strings 
 String  fmtout1; 
  String  fmtout2; 
  String  fmtout3[]; 
  String  dispouta; 
  String  dispoutb; 
  // 
 DecimalFormat dfmt; 
 statf = Simulation.get_statfile(); 
 // 
accum_serv = new double  [num_serv];
 // 
wash_machine = new Server [num_serv];
 // 
 // 
 // create the other active objects of the model 
fmtout3 = new String  [num_serv];
 //  
System.out.println("Number of servers: "+ 
num_serv);
i =  0;
 while ( i < num_serv ) { 
accum_serv [i] =  0.0;
 // added -03-19-2012 
wash_machine [i] = new Server("Wash_machine" + i, i);
 wash_machine [i].start();
 // 
 i++;
 } // endwhile 
 // 		   
genCust = new Arrivals("Arrivals", MeanInterarrival, minServiceDur, maxServiceDur);
 // 
 // display "Workload - car mean inter-arrival: ",  
 //      MeanInterarrival, " mean service: ", maxServiceDur, minServiceDur 
 // 
 genCust.start();
 // starting simulation 
 // 
 sim.start_sim( simPeriod );
 // set formatting to round output to 3 dec places 
dfmt = new DecimalFormat("0.###");
 // 
System.out.println("Total number of cars serviced: "+ 
num_serviced);
statf.println("Total number of cars serviced: "+ 
num_serviced);
statf.flush();
 if ( num_serviced > 0) { 
fmtout1 =  dfmt.format((custWaitTime)/(num_serviced));
System.out.println("Car average wait period: "+ 
fmtout1);
statf.println("Car average wait period: "+ 
fmtout1);
statf.flush();
 // 
fmtout2 =  dfmt.format((custSojournTime)/(num_serviced));
System.out.println("Average period car spends in the shop: "+ 
fmtout2);
statf.println("Average period car spends in the shop: "+ 
fmtout2);
statf.flush();
 // 
 } // endif 
 // machine utilization 
i =  0;
 while ( i < num_serv ) { 
machutil =  (accum_serv [i])/(simPeriod);
fmtout3 [i] =  dfmt.format(machutil);
 // 
statf.println("Machine" + i + " utilization: "+ 
fmtout3 [i]);
statf.flush();
System.out.println("Machine" + i + " utilization: "+ 
fmtout3 [i]);
 i++;
 } // endwhile 
 // 
dispouta =  "Cars serviced: " + num_serviced + "\n" + "Avg wait: " + dfmt.format((custWaitTime)/(num_serviced)) + "\n";
dispoutb =  dispouta + "Avg per in shop: " + dfmt.format((custSojournTime)/(num_serviced));
 if ( guiflag ==  true ) { 
model.println(dispoutb); 
 } // endif 
 System.exit(0); 
 }  // end Main_body 
} // end class/interf Multiples_colas 
